﻿using DLL.MSDM.Entity.Param;
using DLL.Standard.Infrastructure;
using Org.BouncyCastle.Ocsp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml.Serialization;

namespace DLL.NetStandard.FlushRobot.MS.Entity.Param
{
    public class CMS_WR_DeviceConfigParam : CNotifyPropertyChanged
    {
        private string folderPath = @"Param\";

        public CMS_WR_DeviceConfigParam()
        {
            CWR_MotionCardType wrcard = new CWR_MotionCardType();
            CMotionCardType card = new CMotionCardType();
            int devType = 0;
            bool ret = wrcard.ReadParam();
            if (true == ret)
            {
                devType = wrcard.Obj.DeviceType;
            }
            else
            {
                devType = 0;
            }
            string deviceName = "";
            deviceName = CMotionCardType.DevTypeName[devType];
            string str_1 = System.AppDomain.CurrentDomain.BaseDirectory;
            folderPath = str_1 + @"Param\" + deviceName + @"\DeviceParam\";
        }

        private CMS_DeviceConfigParam _Param = new CMS_DeviceConfigParam();

        public CMS_DeviceConfigParam Param
        {
            get { return _Param; }
            set
            {
                if (_Param != value)
                {
                    _Param = value;
                    OnPropertyChanged();
                }
            }
        }

        public bool ReadParam()
        {
            XmlSerializer xmlFormat = new XmlSerializer(typeof(CMS_DeviceConfigParam));
            //string str2 = Application.StartupPath;

            string filePath = folderPath + "MS_DeviceConfigParam.xml";
            if (File.Exists(filePath))
            {
                Stream fstream = File.OpenRead(filePath);
                _Param = (CMS_DeviceConfigParam)xmlFormat.Deserialize(fstream);
                fstream.Close();
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool SaveParam()
        {
            XmlSerializer xmlFormat = new XmlSerializer(typeof(CMS_DeviceConfigParam));
            //现在将数据保存到本地的一个文件中

            string filePath = folderPath + "MS_DeviceConfigParam.xml";

            Stream fstream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None);
            xmlFormat.Serialize(fstream, Param);
            fstream.Close();
            return true;
        }
    }
}